﻿// ----------------------------------------------------------------------
// <author name="Moses Kehengu"/>
// <copyright file="Chart.cs" company="Futures Group International">
//     Copyright statement. All right reserved
// </copyright>
//
// ------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using Telerik.Charting;

namespace ReportingManagement
{
    public partial class Chart : Form
    {
        private Color[] seriesColor = 
        {
            Color.Salmon, Color.Teal, Color.Violet, Color.Red, Color.Lime, 
            Color.Black, Color.Green, Color.Maroon, Color.Navy, Color.Pink, 
            Color.SandyBrown, Color.Magenta, Color.Chocolate, Color.Crimson, 
            Color.Indigo, Color.Khaki, Color.MistyRose, Color.Orange, 
            Color.PaleGreen, Color.Tomato
        };

        public Chart(DataSet dataSet, Dictionary<string, string> reportParameters)
        {
            InitializeComponent();

            ReportChart.Series.Clear();
            ReportChart.PlotArea.XAxis.AutoScale = false;
            ReportChart.PlotArea.Appearance.FillStyle.MainColor = Color.White;
            //ReportChart.PlotArea.Appearance.FillStyle.FillType = Telerik.Charting.Styles.FillType.Solid;
            ReportChart.IntelligentLabelsEnabled = true;
            ReportChart.PlotArea.XAxis.Appearance.TextAppearance.AutoTextWrap = Telerik.Charting.Styles.AutoTextWrap.True;
            try
            {
                string title = reportParameters["Title"];
                string timeFrame = reportParameters["TimeFrame"];
                ReportChart.ChartTitle.TextBlock.Text = title + Environment.NewLine + timeFrame;
                TxtUserChartTitle.Text = ReportChart.ChartTitle.TextBlock.Text;
                this.Text = title;
            }
            catch { }

            try
            {
                DataTable table = dataSet.Tables[0];
                foreach (DataRow row in table.Rows)
                {
                    string name = row["Measure"].ToString();
                    ChartAxisItem i = new ChartAxisItem(name);
                    ReportChart.PlotArea.XAxis.Items.Add(i);
                }
            }
            catch { }

            for (int x = 0; x < dataSet.Tables.Count; x++)
            {
                DataTable dataTable = dataSet.Tables[x];
                ChartSeries series = new ChartSeries();
                series.Name = dataTable.TableName;
                series.Appearance.FillStyle.MainColor = seriesColor[x];
                series.Appearance.FillStyle.FillType = Telerik.Charting.Styles.FillType.Solid;
                foreach (DataRow row in dataTable.Rows)
                {
                    string name = row["Measure"].ToString();
                    double value = 0.0D;
                    try { value = double.Parse(row["Data"].ToString()); }
                    catch { }
                    ChartSeriesItem seriesItem = new ChartSeriesItem();
                    seriesItem.Name = name;
                    seriesItem.YValue = value;
                    series.Items.Add(seriesItem);
                }
                ReportChart.Series.Add(series);
            }
        }

        private void BtnSaveChart_Click(object sender, EventArgs e)
        {
            DialogResult response = SaveFileDialog.ShowDialog(this);
            if (response == DialogResult.OK)
            {
                string filename = SaveFileDialog.FileName;
                string extension = filename.Substring(filename.LastIndexOf(".") + 1);
                ImageFormat imageFormat = null;
                if (extension == "png") imageFormat = ImageFormat.Png;
                if (extension == "gif") imageFormat = ImageFormat.Gif;
                if (extension == "jpg") imageFormat = ImageFormat.Jpeg;
                if (extension == "tif") imageFormat = ImageFormat.Tiff;
                if (extension == "bmp") imageFormat = ImageFormat.Bmp;
                ReportChart.Save(filename, imageFormat);
            }
        }

        private void BtnPrintChart_Click(object sender, EventArgs e)
        {
            ReportChart.Printing.PrintDocument.DefaultPageSettings.Landscape = true;
            ReportChart.Printing.PrintChart(Telerik.WinControls.UI.ChartPrintOptions.ShowPrintPreviewDialog);
        }

        private void TxtUserChartTitle_TextChanged(object sender, EventArgs e)
        {
            ReportChart.ChartTitle.TextBlock.Text = TxtUserChartTitle.Text;
            //ReportChart.Refresh();
        }

        private void btnMore_Click(object sender, EventArgs e)
        {
            CustomizeChart customizeChart = new CustomizeChart(ReportChart.Series);
            customizeChart.SelectedSkin = ReportChart.Skin;
            customizeChart.SelectedColor = ReportChart.PlotArea.Appearance.FillStyle.MainColor;
            customizeChart.SelectedFillType = ReportChart.PlotArea.Appearance.FillStyle.FillType;
            
            DialogResult response = customizeChart.ShowDialog();
            if (response == System.Windows.Forms.DialogResult.OK)
            {
                ReportChart.Skin = customizeChart.SelectedSkin;
                ReportChart.PlotArea.Appearance.FillStyle.MainColor = customizeChart.SelectedColor;
                ReportChart.PlotArea.Appearance.FillStyle.FillType = customizeChart.SelectedFillType; 
                ReportChart.Refresh();
            }
        }
    }
}
